/*     */ package com.zimbra.cs.mailbox;
/*     */ 
/*     */ import com.google.common.base.Objects;
/*     */ import com.google.common.base.Objects.ToStringHelper;
/*     */ import com.zimbra.common.mailbox.Color;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.db.DbMailItem;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class SearchFolder
/*     */   extends Folder
/*     */ {
/*     */   private String mQuery;
/*     */   private String mTypes;
/*     */   private String mSort;
/*     */   private static final String CN_NAME = "name";
/*     */   private static final String CN_QUERY = "query";
/*     */   
/*     */   public SearchFolder(Mailbox mbox, MailItem.UnderlyingData data)
/*     */     throws ServiceException
/*     */   {
/*  43 */     this(mbox, data, false);
/*     */   }
/*     */   
/*     */   public SearchFolder(Mailbox mbox, MailItem.UnderlyingData data, boolean skipCache) throws ServiceException {
/*  47 */     super(mbox, data, skipCache);
/*  48 */     if (this.mData.type != MailItem.Type.SEARCHFOLDER.toByte()) {
/*  49 */       throw new IllegalArgumentException();
/*     */     }
/*     */   }
/*     */   
/*     */   public String getQuery()
/*     */   {
/*  55 */     return this.mQuery == null ? "" : this.mQuery;
/*     */   }
/*     */   
/*     */ 
/*     */   public String getReturnTypes()
/*     */   {
/*  61 */     return this.mTypes == null ? "" : this.mTypes;
/*     */   }
/*     */   
/*     */ 
/*     */   public String getSortField()
/*     */   {
/*  67 */     return this.mSort == null ? "" : this.mSort;
/*     */   }
/*     */   
/*     */ 
/*     */   public boolean isImapVisible()
/*     */   {
/*     */     try
/*     */     {
/*  75 */       return this.mMailbox.getAccount().getBooleanAttr("zimbraPrefImapSearchFoldersEnabled", true);
/*     */     } catch (ServiceException e) {}
/*  77 */     return true;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   boolean canContain(MailItem.Type type)
/*     */   {
/*  86 */     return type == MailItem.Type.SEARCHFOLDER;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   static SearchFolder create(int id, String uuid, Folder parent, String name, String query, String types, String sort, int flags, Color color, MailItem.CustomMetadata custom)
/*     */     throws ServiceException
/*     */   {
/* 119 */     if ((parent == null) || (!parent.canContain(MailItem.Type.SEARCHFOLDER))) {
/* 120 */       throw MailServiceException.CANNOT_CONTAIN();
/*     */     }
/* 122 */     if (!parent.canAccess((short)4)) {
/* 123 */       throw ServiceException.PERM_DENIED("you do not have sufficient permissions on the parent folder");
/*     */     }
/* 125 */     name = validateItemName(name);
/* 126 */     query = validateQuery(query);
/* 127 */     if (parent.findSubfolder(name) != null) {
/* 128 */       throw MailServiceException.ALREADY_EXISTS(name);
/*     */     }
/* 130 */     if ((types != null) && (types.trim().equals(""))) {
/* 131 */       types = null;
/*     */     }
/* 133 */     if ((sort != null) && (sort.trim().equals(""))) {
/* 134 */       sort = null;
/*     */     }
/* 136 */     Mailbox mbox = parent.getMailbox();
/* 137 */     MailItem.UnderlyingData data = new MailItem.UnderlyingData();
/* 138 */     data.uuid = uuid;
/* 139 */     data.id = id;
/* 140 */     data.type = MailItem.Type.SEARCHFOLDER.toByte();
/* 141 */     data.folderId = parent.getId();
/* 142 */     data.parentId = parent.getId();
/* 143 */     data.date = mbox.getOperationTimestamp();
/* 144 */     data.setFlags((flags | Flag.toBitmask(mbox.getAccount().getDefaultFolderFlags())) & Flag.FLAGS_FOLDER);
/* 145 */     data.name = name;
/* 146 */     data.setSubject(name);
/* 147 */     data.metadata = encodeMetadata(color, 1, 1, custom, query, types, sort);
/* 148 */     data.contentChanged(mbox);
/* 149 */     ZimbraLog.mailop.info("Adding SearchFolder %s: id=%d, parentId=%d, parentName=%s.", new Object[] { name, Integer.valueOf(data.id), Integer.valueOf(parent.getId()), parent.getName() });
/*     */     
/* 151 */     new DbMailItem(mbox).create(data);
/*     */     
/* 153 */     SearchFolder search = new SearchFolder(mbox, data);
/* 154 */     search.finishCreation(parent);
/* 155 */     return search;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   void changeQuery(String query, String types, String sort)
/*     */     throws ServiceException
/*     */   {
/* 176 */     if (!isMutable()) {
/* 177 */       throw MailServiceException.IMMUTABLE_OBJECT(this.mId);
/*     */     }
/* 179 */     if (!canAccess((short)2)) {
/* 180 */       throw ServiceException.PERM_DENIED("you do not have sufficient permissions on the search folder");
/*     */     }
/* 182 */     query = validateQuery(query);
/*     */     
/* 184 */     if ((query.equals(this.mQuery)) && (getReturnTypes().equals(types)) && (getSortField().equals(sort))) {
/* 185 */       return;
/*     */     }
/* 187 */     markItemModified(32768);
/* 188 */     this.mQuery = query;
/* 189 */     this.mTypes = types;
/* 190 */     this.mSort = sort;
/* 191 */     saveMetadata();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   protected static String validateQuery(String query)
/*     */     throws ServiceException
/*     */   {
/* 205 */     if (query != null)
/* 206 */       query = StringUtil.stripControlCharacters(query).trim();
/* 207 */     if ((query == null) || (query.equals("")))
/* 208 */       throw ServiceException.INVALID_REQUEST("search query must not be empty", null);
/* 209 */     return query;
/*     */   }
/*     */   
/*     */   void decodeMetadata(Metadata meta) throws ServiceException
/*     */   {
/* 214 */     super.decodeMetadata(meta);
/* 215 */     this.mQuery = meta.get("q");
/* 216 */     this.mTypes = meta.get("t", null);
/* 217 */     this.mSort = meta.get("s", null);
/*     */   }
/*     */   
/*     */   Metadata encodeMetadata(Metadata meta) {
/* 221 */     return encodeMetadata(meta, this.mRGBColor, this.mMetaVersion, this.mVersion, this.mExtendedData, this.mQuery, this.mTypes, this.mSort);
/*     */   }
/*     */   
/*     */   private static String encodeMetadata(Color color, int metaVersion, int version, MailItem.CustomMetadata custom, String query, String types, String sort) {
/* 225 */     MailItem.CustomMetadata.CustomMetadataList extended = custom == null ? null : custom.asList();
/* 226 */     return encodeMetadata(new Metadata(), color, metaVersion, version, extended, query, types, sort).toString();
/*     */   }
/*     */   
/*     */   static Metadata encodeMetadata(Metadata meta, Color color, int metaVersion, int version, MailItem.CustomMetadata.CustomMetadataList extended, String query, String types, String sort) {
/* 230 */     meta.put("q", query);
/* 231 */     meta.put("t", types);
/* 232 */     meta.put("s", sort);
/* 233 */     return MailItem.encodeMetadata(meta, color, null, metaVersion, version, extended);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public String toString()
/*     */   {
/* 242 */     Objects.ToStringHelper helper = Objects.toStringHelper(this);
/* 243 */     appendCommonMembers(helper);
/* 244 */     helper.add("name", getName());
/* 245 */     helper.add("query", getQuery());
/* 246 */     return helper.toString();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/SearchFolder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */